package com.dgut.crm.demo.web;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dgut.crm.common.util.ExtAjaxResponse;
import com.dgut.crm.common.util.ExtPageable;
import com.dgut.crm.demo.entity.User;
import com.dgut.crm.demo.entity.dto.UserQueryDTO;
import com.dgut.crm.demo.service.IUserService;


@Controller
@RequestMapping("/user")
public class UserContreller 
{
	@Autowired
	private IUserService userService;
	
	
	@RequestMapping("/saveOrUpdate")
	public @ResponseBody ExtAjaxResponse saveOrUpdate(User user) 
	{
		try {
			userService.saveOrUpdate(user);
			if(null!=user) {
				userService.saveOrUpdate(user);
			}
			return new ExtAjaxResponse(true,"操作成功！");
		} catch (Exception e) {
			return new ExtAjaxResponse(false,"操作失败！");
		}
		
	}
	@RequestMapping("/delete")
	public @ResponseBody ExtAjaxResponse delete(@RequestParam Long id) 
	{
		try {
			User user =  userService.findOne(id);
			if(null!=user) {
				userService.delete(user);
			}
			return new ExtAjaxResponse(true,"操作成功！");
		} catch (Exception e) {
			return new ExtAjaxResponse(false,"操作失败！");
		}

	}
	@RequestMapping("/deleteUsers")
	public @ResponseBody ExtAjaxResponse deleteUsers(@RequestParam() Long[] ids) 
	{
		try {
			List<Long> idLists = Arrays.asList(ids); 
			if(null!=idLists) {
				userService.delete(idLists);
			}
			return new ExtAjaxResponse(true,"操作成功！");
		} catch (Exception e) {
			return new ExtAjaxResponse(false,"操作失败！");
		}

	}
	
	@RequestMapping("/findOne")
	public @ResponseBody User findOne(@RequestParam Long id) 
	{
		User user =  userService.findOne(id);
		
		return user;
	}
	
	@RequestMapping("/findPage")
	public @ResponseBody Page <User>findPage(UserQueryDTO userQueryDTO,ExtPageable pageable) 
	{
		Page<User>page =  userService.findAll(UserQueryDTO.getSpecification(userQueryDTO), pageable.getPageable());
		return page;
	}

	
}
